﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.QixingManage;
using YiSha.Business.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Admin.Web.Model;
using Newtonsoft.Json;

namespace YiSha.Admin.Web.Areas.QixingManage.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-25 18:51
    /// 描 述：入群配置控制器类
    /// </summary>
    [Area("QixingManage")]
    public class GroupChatJoinWayController :  BaseController
    {
        private GroupChatJoinWayBLL groupChatJoinWayBLL = new GroupChatJoinWayBLL();

        #region 视图功能
        [AuthorizeFilter("qixing:groupchatjoinway:view")]
        public ActionResult GroupChatJoinWayIndex()
        {
            return View();
        }

        public ActionResult GroupChatJoinWayForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("qixing:groupchatjoinway:search")]
        public async Task<ActionResult> GetListJson(GroupChatJoinWayListParam param)
        {
            TData<List<GroupChatJoinWayEntity>> obj = await groupChatJoinWayBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("qixing:groupchatjoinway:search")]
        public async Task<ActionResult> GetPageListJson(GroupChatJoinWayListParam param, Pagination pagination)
        {
            TData<List<GroupChatJoinWayEntity>> obj = await groupChatJoinWayBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<GroupChatJoinWayEntity> obj = await groupChatJoinWayBLL.GetEntity(id);
            return Json(obj);
        }

        public async Task<ActionResult> GetChatFormJson(string chatid)
        {
            TData<GroupChatJoinWayEntity> obj = await groupChatJoinWayBLL.GetChatFormJson(chatid);
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("qixing:groupchatjoinway:add,qixing:groupchatjoinway:edit")]
        public async Task<ActionResult> SaveFormJson(GroupChatJoinWayEntity entity)
        {
            var res = "";
            try
            {
                //调用远程http
                var url = "";
                var postjson = "";
                if (string.IsNullOrEmpty(entity.config_id))
                {
                    url = GlobalContext.SystemConfig.WebApiSite + "/WeCom/AddContactWay";
                    GroupChat_AddJoinWay postdata = new GroupChat_AddJoinWay();
                    postdata.auto_create_room = entity.auto_create_room.Value;
                    postdata.chat_id = entity.chat_id;
                    postdata.remark = entity.remark;
                    postdata.room_base_id = entity.room_base_id != null ? entity.room_base_id.Value : 0;
                    postdata.room_base_name = entity.room_base_name;
                    postdata.scene = entity.scene.Value;
                    postdata.state = "";
                    postjson = System.Text.Json.JsonSerializer.Serialize(postdata);
                }
                else
                {
                    url = GlobalContext.SystemConfig.WebApiSite + "/WeCom/UpdateContactWay";
                    GroupChat_UpdateJoinWay postdata = new GroupChat_UpdateJoinWay();
                    postdata.auto_create_room = entity.auto_create_room.Value;
                    postdata.chat_id = entity.chat_id;
                    postdata.remark = entity.remark;
                    postdata.config_id = entity.config_id;
                    postdata.room_base_id = entity.room_base_id.Value;
                    postdata.room_base_name = entity.room_base_name;
                    postdata.scene = entity.scene.Value;
                    postdata.state = "";
                    postjson = System.Text.Json.JsonSerializer.Serialize(postdata);
                }

                res = HttpHelper.Post(postjson, url);

                var result = System.Text.Json.JsonSerializer.Deserialize<WebApiCallBack>(res);

                TData<string> obj = new TData<string>();

                if (result.code == 200)
                {
                    obj.Tag = 1;
                    obj.Message = result.msg;
                }
                else
                {
                    obj.Tag = result.code;
                    obj.Message = result.msg;
                }
                return Json(obj);
            }
            catch (Exception e)
            {
                TData<string> obj = new TData<string>();
                obj.Tag = 0;
                obj.Message = e.Message;
                obj.Description = res;
                return Json(obj);
            }
        }


        public async Task<ActionResult> PullGroupChatForm()
        {
            var url = GlobalContext.SystemConfig.WebApiSite + "/WeCom/PullGroupChat";
            var res = HttpHelper.HttpPost(url,"", "application/json");
            var result = System.Text.Json.JsonSerializer.Deserialize<WebApiCallBack>(res);
            TData<string> obj = new TData<string>();
            if (result.code == 200)
            {
                obj.Tag = 1;
                obj.Message = result.msg;
            }
            else
            {
                obj.Tag = result.code;
                obj.Message = result.msg;
            }
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("qixing:groupchatjoinway:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await groupChatJoinWayBLL.DeleteForm(ids);
            return Json(obj);
        }


        [HttpPost]
        [AuthorizeFilter("qixing:groupchatjoinway:delete")]
        public async Task<ActionResult> DeleteConfigIdFormJson(string configId)
        {
            var url = GlobalContext.SystemConfig.WebApiSite + "/WeCom/DelContactWay";
            var body =new { configId=configId};
            var res = HttpHelper.HttpPost(url, System.Text.Json.JsonSerializer.Serialize(body), "application/json");
            var result = System.Text.Json.JsonSerializer.Deserialize<WebApiCallBack>(res);
            TData<string> obj = new TData<string>();
            if (result.code == 200)
            {
                obj.Tag = 1;
                obj.Message = result.msg;
            }
            else
            {
                obj.Tag = result.code;
                obj.Message = result.msg;
            }
            return Json(obj);
        }
        #endregion
    }
}
